<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<#include "/common/resources.ftl"/>
<script type="text/javascript">
	$(function(){
	    
		window.m_datagrid = $('#m_datagrid').datagrid({
			url:'${ctx}/qfbao/preloanorder/list',
			onLoadSuccess:yixun.closeProgress,
			onBeforeLoad:yixun.progress,
			queryParams:$('#load_list_search').serializeObject(),
			loadMsg:null,
			singleSelect:true,
			fitColumns:true,
			rownumbers:true,
			pagination:true,
			border:'false',
			toolbar:'#m_toolbar'
		});
		
		
		
		
	
		//按条件查询用户
		window.searchData = function () {
			m_datagrid.datagrid('load',$('#load_list_search').serializeObject());
		}
		
		window.openFpsh = function(id){
		   $("#pre_cus_id").val(id);
		   //查询当前客户已分配的商户
		   $.ajax({
				url:"${ctx}/qfbao/loanorder/queryDistributionCompany",
				type: "POST",
				data:{
					id:id
				},
				dataType:"json",
				success:function(rs){
				    $("#companyIds").val(rs.companyIds);
			        $("#companyNames").val(rs.companyNames);
					distributionGrid();
                    $("#fpsh").window('open');
				}
		   })
		   
		}
		
		$('#fpsh').window('close');
		getCompany();
	});
	mShowButton = function (value,row,index){
		var btns = [];
		if(row.status=="1"){
		    btns.push('<button class="green_btn" onclick="javascript:openFpsh('+value+');void(0);">分配商户</button>');
		}
		btns.push('<button class="green_btn" onclick="javascript:followList('+value+');void(0);">查看跟进</button>');
		return btns.join('');
	}
	
   statusName = function (value,row,index){
		var statusName="";
		if(value=="1"){
		   statusName="未分配";
		}else if(value=="2"){
		   statusName="已分配";
		}
		return statusName;
	}
	
	function fpsh(){
	    var pre_cus_id=$("#pre_cus_id").val();
	    var newCompanyIds=$("#newCompanyIds").val();
	    if(pre_cus_id==""){
	       $.messager.alert('警告','ID错误');  
	       return;
	    }
	    
	    if(newCompanyIds==""){
	       $.messager.alert('警告','请选择商户');  
	       return;
	    }
	    
	    $.ajax({
			url:"${ctx}/qfbao/preloanorder/fpMerchant",
			type: "POST",
			data:{
				id:pre_cus_id,
				companyIds:newCompanyIds,
				flag:0
			},
			dataType:"json",
			success:function(rs){
				if(rs.isSuccess=="T"){
				    $('#fpsh').window('close');
					m_datagrid.datagrid('load',$('#load_list_search').serializeObject());
				}else{
				    $.messager.alert('错误',rs.msg);  
				}
			}
		})
	   
	    
	}
	
	function getCompany(){
		$('#companyId').combobox({    
		    url:'${ctx}/jr/company/getList?companyType=2',    
		    valueField:'id',    
		    textField:'companyName',
		    onSelect:function(rec){
		    	queryProduct(rec.id);
		    }  
		});
	}
	
	function queryProduct(companyId){
	    $('#productId').combobox({
      		url:'${ctx}/qfbao/product/queryProductByCompanyId?companyId='+companyId,    
	  		valueField:'id',    
	  		textField:'name'
   		});
	}
	
   cusSourceName = function (value,row,index){
		var statusName="";
		if(value=="1"){
		   statusName="PC端";
		}else if(value=="2"){
		   statusName="微信端";
		}else if(value=="3"){
		   statusName="用户推荐";
		}
		return statusName;
	} 
	
	termFormat = function (value,row,index){
		var termName=value;
		if(row.termUnit=="0"){
		   termName=termName+"日";
		}else if(row.termUnit=="1"){
		   termName=termName+"个月";
		}else if(row.termUnit=="2"){
		   termName=termName+"年";
		}
		return termName;
	} 
	
	function followList(id){
	   window.location.href = '${ctx}/qfbao/preloanorder/followList?id='+id;
	}
	
	function jumpOrderList(id){
	   window.location.href = '${ctx}/qfbao/loanorder/manage?partnerId='+id;
	}
	
	fdDetailFun = function (value,row,index){
		var btns = [];
		btns.push('<button class="green_btn" onclick="jumpOrderList('+value+')">分单详情</button>');
		return btns.join('');
	}
	
	function distributionGrid(){
	    $('#div_datagrid').datagrid({
			url:'${ctx}/jr/company/distributionList',
			onLoadSuccess:yixun.closeProgress,
			onBeforeLoad:yixun.progress,
			queryParams:$('#distribution_form').serializeObject(),
			loadMsg:'加载中',
			singleSelect:true,
			fitColumns:true,
			rownumbers:false,
			pagination:true,
			border:'false',
			onClickRow:function(index,row){
			  var companyIds=$("#companyIds").val();
			  var newCompanyIds=$("#newCompanyIds").val();
			  var companyNames=$("#companyNames").val();
			  var rows=$('#div_datagrid').datagrid("getSelections");
			  for(var i=0;i<rows.length;i++){
			      var id=rows[i].id;
			      var companyName=rows[i].companyName;
			      if(newCompanyIds==""){
			         newCompanyIds=id;
			      }else{
			         newCompanyIds=newCompanyIds+","+id; 
			      }
			      if(companyIds==""){
			         companyIds=id;
			      }else{
			         companyIds=companyIds+","+id; 
			      }
			      companyNames=companyNames+companyName+";"; 
			  }
			  $("#companyIds").val(companyIds);
			  $("#companyNames").val(companyNames);
			  $("#newCompanyIds").val(newCompanyIds);
			  $('#div_datagrid').datagrid('load',$('#distribution_form').serializeObject());
			}
		});
	}
</script>
</head>
<body style="background-color:white;">

<div id="m_toolbar" class="datagrid-toolbar" style="overflow: hidden;height: auto;margin: 15px 0px;background-color: white;border-bottom: none;" border="false">
  <form id="load_list_search" onsubmit="return false;">
  <div class="fm_sty" style="margin: 20px 0px 0px;">

   	<div class="ipt_line">
	    <span class="ipt_title">姓名</span>
		<input type="text" name="applicant" class="ipt_226" />
	</div>
	
   	<div class="ipt_line">
	    <span class="ipt_title">手机号码</span>
		<input type="text" name="phone" class="ipt_226" />
	</div>
	
	<input type="hidden" name="cusType" value="1" />

	<div class="ipt_line_long">
        <button class="red_100" onclick="searchData()">查询</button>
	</div>    
  </div>
  </form>
</div>

<!-- 查找结果 -->
<table style="margin:0px 20px;" id="m_datagrid">
	<thead>
		<tr>
			<th field="applicant"  height="40" >姓名</th>
			<th field="phone"  height="40" >手机号码</th>
			<th field="applyAmount" height="40" >意向金额(万)</th>
			<th data-options="field:'term',formatter:termFormat,align:'center'" height="40" >贷款期限</th>
            <th data-options="field:'cusSource',formatter:cusSourceName,align:'center'"  height="40" >客户来源</th>
            <th field="applyName" height="40" >报备人/推荐人</th>
			<th field="applyDate" height="40" >申请时间</th>
			<th data-options="field:'id',formatter:mShowButton,align:'center'" width="60">操作</th>
		</tr>
	</thead>
</table>

<div id="fpsh" class="easyui-dialog" title="分配商户" style="width:750px;height:520px;padding:10px 5px 10px 5px;"  data-options="iconCls:'icon-save',resizable:true,modal:true" >   
        	<div class="easyui-layout"  style="width:700px;height:460px;">
        	        <div data-options="region:'north'" style="height:150px">
        	            <form id="distribution_form">
        	            <div class="ipt_line">
						    <span class="ipt_title">商户性质:</span>
							<select class="easyui-combobox" style="width:200px;" name="companyType">
							   <option value="2">金融商户</option>
							</select>
						</div>
						<div class="ipt_line">
						    <span class="ipt_title">选择商户:</span>
						    <textarea rows=5  class="textarea" style="height:90px;width:600px" id="companyNames"></textarea>
						</div>
						<input type="hidden" name="companyIds" id="companyIds">
						<input type="hidden" id="pre_cus_id">
						<input type="hidden" id="newCompanyIds">
						
						</form>
        	        </div>
			        <div data-options="region:'center',title:'可分单商户列表',iconCls:'icon-ok'">
						 <table class="easyui-datagrid" id="div_datagrid">
							<thead>
								<tr>
									<th field="companyName" height="40" >商户名称</th>
									<th field="weekCount"  height="40" >当月分配次数</th>
									<th field="monthCount" height="40" >本周分配次数</th>
									<th data-options="field:'id',formatter:fdDetailFun,align:'center'" width="60">详情</th>
								</tr>
							</thead>
						</table>
   			        </div>
   			         
   			        <div region="south" border="false" style="text-align:center;height:30px;line-height:30px;">
						<a href="#" class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="fpsh()">提交</a>
						<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="javascript:$('#fpsh').window('close')">取消</a>
					</div>
   			</div>
</div>

</body>
</html>
